```html
<!DOCTYPE html>
<html>
<head>
    <title>AI对话助手</title>
    <style>
        /* 保留原样式 */
        body {
            font-family: Arial, sans-serif;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }

        .chat-container {
            display: flex;
            flex-direction: column;
            height: 90vh;
            border: 1px solid #ddd;
            border-radius: 8px;
            background-color: white;
        }

        .messages {
            flex: 1;
            overflow-y: auto;
            padding: 20px;
            display: flex;
            flex-direction: column;
            gap: 15px;
        }

        .message {
            max-width: 70%;
            padding: 12px 16px;
            border-radius: 15px;
            margin: 5px 0;
        }

        .user-message {
            background-color: #007bff;
            color: white;
            align-self: flex-end;
        }

        .ai-message {
            background-color: #e9ecef;
            color: #212529;
            align-self: flex-start;
            word-wrap: break-word; /* 防止长文本溢出 */
        }

        .input-container {
            padding: 20px;
            border-top: 1px solid #ddd;
            display: flex;
            gap: 10px;
        }

        inputtype="text"] {
            flex: 1;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 25px;
            outline: none;
        }

        button {
            padding: 12px 24px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 25px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #0056b3;
        }

        .typing-indicator {
            display: none;
            padding: 8px 16px;
            background-color: #e9ecef;
            border-radius: 15px;
            align-self: flex-start;
            color: #6c757d;
        }
    </style>
</head>
<body>
<div class="chat-container">
    <div class="messages" id="messages">
        <div class="message ai-message">您好！我是AI助手，有什么可以帮助您？</div>
    </div>
    <div class="input-container">
        <input type="text" id="userInput" placeholder="输入您的问题...">
        <button onclick="sendMessage()">发送</button>
    </div>
</div>

<script>
    let currentAiMessage = null;
    let isStreaming = false;
    let sessionId = Math.random().toString(36).substr(2, 9); // 生成唯一的会话ID

    function sendMessage() {
        const userInput = document.getElementById('userInput');
        const message = userInput.value.trim();

        if (!message || isStreaming) return;

        userInput.value = '';
        createUserMessage(message);
        createAiMessage();
        isStreaming = true;

        fetch(`stream/conversation?sessionId=${sessionId}`, { // 在URL中传递会话ID
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ message: message })
        })
            .then(response => {
                const reader = response.body.getReader();
                const decoder = new TextDecoder();
                let buffer = '';

                function read() {
                    return reader.read().then(({ done, value }) => {
                        if (done) {
                            isStreaming = false;
                            return;
                        }

                        buffer += decoder.decode(value, { stream: true });

                        // 处理SSE格式数据
                        const lines = buffer.split('\n');
                        buffer = lines.pop() || ''; // 保留未完成的行

                        lines.forEach(line => {
                            if (line.startsWith('data:')) {
                                const content = line.replace('data:', '').trim();
                                appendToAiMessage(content);
                            }
                        });

                        return read();
                    });
                }

                return read();
            })
            .catch(error => {
                console.error('请求失败:', error);
                appendToAiMessage("\n服务异常，请稍后重试]");
                isStreaming = false;
            });
    }

    function createUserMessage(text) {
        const container = document.getElementById('messages');
        const div = document.createElement('div');
        div.className = 'message user-message';
        div.textContent = text;
        container.appendChild(div);
        container.scrollTop = container.scrollHeight;
    }

    function createAiMessage() {
        const container = document.getElementById('messages');
        currentAiMessage = document.createElement('div');
        currentAiMessage.className = 'message ai-message';
        container.appendChild(currentAiMessage);
        container.scrollTop = container.scrollHeight;
    }

    function appendToAiMessage(htmlContent) {
        if (currentAiMessage) {
            // 使用innerHTML来支持富文本显示
            // 注意：这里直接插入HTML内容存在安全风险，实际应用中需要对HTML内容进行过滤或转义
            currentAiMessage.innerHTML += htmlContent.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');

            // 为了安全起见，这里只是进行了简单的HTML转义，实际应用中可能需要更复杂的处理
            // 例如，使用DOMPurify等库来过滤HTML内容

            // 保持滚动条在底部
            const container = document.getElementById('messages');
            container.scrollTop = container.scrollHeight;
        }
    }

    // 支持回车键发送
    document.getElementById('userInput').addEventListener('keypress', (e) => {
        if (e.key === 'Enter') {
            sendMessage();
        }
    });
</script>
</body>
</html>
